package com.taomeng.dao.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.taomeng.dao.ClassDao;
import com.taomeng.entity.Cls;
import com.taomeng.entity.ClsInfo;
import com.taomeng.entity.PageBean;
import com.taomeng.util.JdbcTemplateSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class ClassDaoImpl implements ClassDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private JdbcTemplateSupport jdbcTemplateSupport;

    @Override
    public List<Cls> findAll() {
        String sql = "select * from class";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper(Cls.class));
    }

    @Override
    public PageBean<ClsInfo> findByPage(int page, int limit, String key, Long departmentId, Long userId) {
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT \n" +
                "\tclass.id,\n" +
                "\tclass.num,\n" +
                "\tclass.name,\n" +
                "\tt_user.name banZhuRenName,\n" +
                "\tdepartment.name departmentName\n" +
                "FROM\n" +
                "`class`\n" +
                "LEFT JOIN\n" +
                "department\n" +
                "on\n" +
                "class.departmentId=department.id\n" +
                "LEFT JOIN\n" +
                "t_user\n" +
                "on\n" +
                "class.banZhuRenId=t_user.id\n" +
                "where\n" +
                "1=1 ");
        Page<ClsInfo> pagination = PageHelper.startPage(page, limit);

        RowMapper<ClsInfo> clsInfoRowMapper = new RowMapper<ClsInfo>() {
            @Override
            public ClsInfo mapRow(ResultSet resultSet, int i) throws SQLException {
                ClsInfo clsInfo = new ClsInfo();
                clsInfo.setId(resultSet.getInt("id"));
                clsInfo.setNum(resultSet.getString("num"));
                clsInfo.setName(resultSet.getString("name"));
                clsInfo.setBanZhuRenName(resultSet.getString("banZhuRenName"));
                clsInfo.setDepartmentName(resultSet.getString("departmentName"));
                return clsInfo;
            }
        };
        try {
            if (null != key) {
                key = "%" + key + "%";
                sb.append(" and class.num like ").append(key);
            }
            if (null != departmentId)
                sb.append(" and department.id=").append(departmentId);
            else if (null != userId)
                sb.append(" and t_user.id=").append(userId);
            return jdbcTemplateSupport.queryForPage(sb.toString(), pagination, clsInfoRowMapper);
        } catch (
                Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void add(Cls cls) {
        String sql = "insert into class values(null,?,?,?,?)";
        Object[] params = {
                cls.getName(),
                cls.getNum(),
                cls.getBanZhuRenId(),
                cls.getDepartmentId()
        };
        try {
            jdbcTemplate.update(sql, params);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void del(String id) {
        String sql = "delete from class where id = ?";
        Object[] params = {id};
        try {
            jdbcTemplate.update(sql, params);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean update(Cls cls) {
        String sql = "update class set name = ? , num = ? , banZhuRenId = ? , departmentId = ? where id = ?";
        Object[] params = {
                cls.getNum(),
                cls.getNum(),
                cls.getBanZhuRenId(),
                cls.getDepartmentId(),
                cls.getId()
        };

        try {
            return jdbcTemplate.update(sql, params) == 1;
        } catch (DataAccessException e) {
            e.printStackTrace();
        }

        return false;
    }
}
